package com.cloudptt.wp.ui.call.group.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudptt.api.comm.CommDlg;
import com.cloudptt.api.product.func.call.CallConst;
import com.cloudptt.api.product.func.call.group.fsm.video.GroupCallMemberInfo;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.R;
import com.cloudptt.wp.ui.call.group.GroupCallFragment;
import com.mcptt.db.model.GroupMembInfo;

import java.util.Collections;
import java.util.List;

public class GroupCallMemberAdapter extends BaseAdapter {

    private Context ctx = null;
    private boolean isView = false;
    private int selectedPos = 0;
    private List<GroupCallMemberInfo> groupCallMemberInfoList = null;
    private  GroupCallFragment.OnCallEvents callEvents = null;

    public GroupCallMemberAdapter(Context ctx, List<GroupCallMemberInfo> groupCallMemberInfoList, GroupCallFragment.OnCallEvents callEvents) {
        this.ctx = ctx;
        this.groupCallMemberInfoList = groupCallMemberInfoList;
        this.callEvents = callEvents;
        Log.debug(this.getClass(),"new groupCallMemberInfoList = " + groupCallMemberInfoList);
        Collections.sort(this.groupCallMemberInfoList);
    }

    @Override
    public int getCount() {
        if(groupCallMemberInfoList != null){
            return groupCallMemberInfoList.size();
        }else{
            return 0;
        }
    }

    public void refresh(List<GroupCallMemberInfo> groupCallMemberInfoList){
        Log.debug(this.getClass(),"refresh groupCallMemberInfoList = " + groupCallMemberInfoList);
        this.groupCallMemberInfoList = groupCallMemberInfoList;
        Collections.sort(this.groupCallMemberInfoList);
    }

    @Override
    public Object getItem(int position) {
        return groupCallMemberInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        GroupCallMemberInfo info = groupCallMemberInfoList.get(position);
        Log.debug(this.getClass(),"getView info =  "+ info);
        GroupMembInfo addrInfo = info.getGroupMembInfo();
        String descStr = addrInfo.getDscp();
        if (convertView == null)
        {
            LayoutInflater inflater = LayoutInflater.from(ctx);
            convertView = inflater.inflate(R.layout.activity_group_call_member_item, null);

            viewHolder = new ViewHolder();
            viewHolder.faceIv = (ImageView) convertView.findViewById(R.id.faceIv);
            viewHolder.nameTv = (TextView) convertView.findViewById(R.id.nameTv);
            viewHolder.audienceTv = (TextView) convertView.findViewById(R.id.audienceTv);
            viewHolder.videoUploadIv = (ImageView) convertView.findViewById(R.id.videoUploadIv);

            convertView.setTag(viewHolder);
        }
        else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        boolean isConnected = info.getGroupMembInfo().isConnected();
        Log.debug(this.getClass(),"getView isConnected =  "+ isConnected);
        if(isConnected)
        {
            if(addrInfo.isSys()
                    || addrInfo.isDac())
            {
                viewHolder.faceIv.setImageResource(R.drawable.icon_main_user_dac_face);

            }else if(addrInfo.isFuncType())
            {
                viewHolder.faceIv.setImageResource(R.drawable.icon_main_user_func_face);

            }
            else{
                viewHolder.faceIv.setImageResource(R.drawable.icon_main_user_face);
            }
        }
        else
        {
            if(addrInfo.isSys()
                    || addrInfo.isDac())
            {
                viewHolder.faceIv.setImageResource(R.drawable.icon_main_user_dac_offline_face);

            }else if(addrInfo.isFuncType())
            {
                viewHolder.faceIv.setImageResource(R.drawable.icon_main_user_func_offline_face);

            }
            else{
                viewHolder.faceIv.setImageResource(R.drawable.icon_main_user_offline_face);
            }
        }
        boolean isRecv = info.isRecv();
        if(isRecv)
        {
            viewHolder.audienceTv.setVisibility(View.VISIBLE);
        }
        else
        {
            viewHolder.audienceTv.setVisibility(View.INVISIBLE);
        }
        boolean isTrans = info.isTrans();
        if(isTrans)
        {
            viewHolder.videoUploadIv.setVisibility(View.VISIBLE);
            Log.debug(this.getClass(),"getView isView =  "+ isView + " Pos = " + selectedPos + "_" + position);
            if(isView
                && selectedPos == position)
            {
                viewHolder.videoUploadIv.setImageResource(R.drawable.btn_group_call_video_view_normal);
            }
            else
            {
                viewHolder.videoUploadIv.setImageResource(R.drawable.btn_group_call_video_view_pressed);
            }
        }
        else
        {
            viewHolder.videoUploadIv.setVisibility(View.INVISIBLE);
        }
        viewHolder.videoUploadIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String jsonStr = info.getJsonStr();
                if(isView) {
//                    CommDlg.showToast(ctx,R.string.group_call_view_only_one);
                    if (callEvents != null) {
                        Log.debug(this.getClass(), "isView = false jsonStr = " + jsonStr);
                        callEvents.doUserAction(CallConst.BtnAction_Recv_End, jsonStr);
                        selectedPos = -1;
                        isView = false;
                    }
                }
                else
                {
                    if (callEvents != null) {
                        Log.debug(this.getClass(), "isView = true jsonStr = " + jsonStr);
                        callEvents.doUserAction(CallConst.BtnAction_Recv_Begin, jsonStr);
                        selectedPos = position;
                        isView = false;
                    }
                }
            }
        });
        viewHolder.nameTv.setText(descStr);
        return convertView;
    }



    public boolean isView() {
        return isView;
    }

    public void setIsView(boolean view) {
        isView = view;
        Log.debug(this.getClass(), "setIsView isView = " + isView);
    }

    public int getSelectedPos() {
        return selectedPos;
    }

    public GroupCallMemberInfo getSelectedOb() {
        return groupCallMemberInfoList.get(selectedPos);
    }

    public void setSelectedPos(int selectedPos) {
        this.selectedPos = selectedPos;
    }

    private class ViewHolder {
        public ImageView faceIv;
        public TextView nameTv;
        public TextView audienceTv;
        public ImageView videoUploadIv;
    }
}
